//
//  BMAQuestionsExoViewController.m
//  Autismus
//
//  Created by Nicolas CHENG on 03/12/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "BMAQuestionsExoViewController.h"
#import "BMAMovieViewController.h"
#include <stdlib.h>

@implementation BMAQuestionsExoViewController

@synthesize photo;
@synthesize illustration1;
@synthesize illustration2;
@synthesize illustration3;
@synthesize illustration4;
@synthesize level;
@synthesize currentEmotion;
@synthesize emotionIllustration1;
@synthesize emotionIllustration2;
@synthesize emotionIllustration3;
@synthesize emotionIllustration4;
@synthesize consecutiveWin;
@synthesize consecutiveLoss;
@synthesize congratsView;

-(IBAction)goBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self initGame];
    [self refreshGame];
    
    congratsView.userInteractionEnabled = NO;
    congratsView.alpha = 0;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
	return YES;
}

- (void)initGame {
    self.level = 1;

}

- (void)refreshGame {
    
    BMAAppManager *manager = [BMAAppManager sharedInstance];
    
    int nbEmotion = 6;
    
    int goodResult = 0;
    int emotion_index = arc4random() % nbEmotion;
    currentEmotion = emotion_index;
    
    // display a random photo
    [photo setImage:[manager randomPhotoFor:emotion_index]];

    // en fonction du level, prendre nb illustration
    
    if (level == 1) {
        goodResult = arc4random() % 2;
        illustration1.hidden = NO;
        illustration2.hidden = NO;
        illustration3.hidden = YES;
        illustration4.hidden = YES;
        
        int emotion1 = 0;
        int emotion2 = 0;
        
        int emotion2_index = arc4random() % nbEmotion;
        while (emotion2_index == currentEmotion) {
            emotion2_index = arc4random() % nbEmotion;
        }
        
        if (goodResult == 0) {
            emotion1 = emotion_index;
            emotion2 = emotion2_index;
        } else {
            emotion1 = emotion2_index;
            emotion2 = emotion_index;
        }
        
        [illustration1 setImage:[manager illustrationFor:emotion1] forState:UIControlStateNormal];
        emotionIllustration1 = emotion1;
                
        [illustration2 setImage:[manager illustrationFor:emotion2] forState:UIControlStateNormal];
        emotionIllustration2 = emotion2;
        
        DLOG(@"currentEmotion = %d", currentEmotion);
        DLOG(@"emotionIllustration1 = %d", emotionIllustration1);
        DLOG(@"emotionIllustration2 = %d", emotionIllustration2);
    } else if (level == 2) {
        goodResult = arc4random() % 4;
        illustration1.hidden = NO;
        illustration2.hidden = NO;
        illustration3.hidden = NO;
        illustration4.hidden = NO;
        
        int emotion1 = 0;
        int emotion2 = 0; 
        int emotion3 = 0;
        int emotion4 = 0;

//        int emotion1_index = arc4random() % nbEmotion;
        
        int emotion2_index = arc4random() % nbEmotion;
        while (emotion2_index == currentEmotion) {
            emotion2_index = arc4random() % nbEmotion;
        }
        
        int emotion3_index = arc4random() % nbEmotion;
        while (emotion3_index == currentEmotion || emotion3_index == emotion2_index) {
            emotion3_index = arc4random() % nbEmotion;
        }
        
        int emotion4_index = arc4random() % nbEmotion;
        while (emotion4_index == currentEmotion || emotion4_index == emotion2_index || emotion4_index == emotion3_index) {
            emotion4_index = arc4random() % nbEmotion;
        }
        
        if (goodResult == 0) {
            emotion1 = emotion_index;
            emotion2 = emotion2_index;
            emotion3 = emotion3_index;
            emotion4 = emotion4_index;
        } else if (goodResult == 1) {
            emotion1 = emotion2_index;
            emotion2 = emotion_index;
            emotion3 = emotion3_index;
            emotion4 = emotion4_index;
        } else if (goodResult == 2) {
            emotion1 = emotion3_index;
            emotion2 = emotion2_index;
            emotion3 = emotion_index;
            emotion4 = emotion4_index;
        } else if (goodResult == 3) {
            emotion1 = emotion4_index;
            emotion2 = emotion2_index;
            emotion3 = emotion3_index;
            emotion4 = emotion_index;
        }
        
        
        [illustration1 setImage:[manager illustrationFor:emotion1] forState:UIControlStateNormal];
        emotionIllustration1 = emotion1;
        
        [illustration2 setImage:[manager illustrationFor:emotion2] forState:UIControlStateNormal];
        emotionIllustration2 = emotion2;
        
        [illustration3 setImage:[manager illustrationFor:emotion3] forState:UIControlStateNormal];
        emotionIllustration3 = emotion3;
        
        [illustration4 setImage:[manager illustrationFor:emotion4] forState:UIControlStateNormal];
        emotionIllustration4 = emotion4;
        
        DLOG(@"currentEmotion = %d", currentEmotion);
        DLOG(@"emotionIllustration1 = %d", emotionIllustration1);
        DLOG(@"emotionIllustration2 = %d", emotionIllustration2);
        DLOG(@"emotionIllustration3 = %d", emotionIllustration3);
        DLOG(@"emotionIllustration4 = %d", emotionIllustration4);
        
    }
}

//- (IBAction) buttonPhotoPushed {
//    
//}

- (void) result:(bool)result {
    DLOG(@"result");
    
    if (result == FALSE)
    {
        // loose
        
        self.consecutiveWin = 0;
        self.consecutiveLoss++;
        if (consecutiveLoss == 8) {
            consecutiveLoss = 0;
            self.level--;
            if (self.level < 1) {
                self.level = 1;
            }
            [self refreshGame];
        }
        
        NSLog(@"perdu");
        
//        return;
    } else {
        // win
        
        self.consecutiveLoss = 0;
        self.consecutiveWin++;
        if (consecutiveWin == 5) {
            consecutiveWin = 0;
            self.level++;
            if (self.level > 2) {
                self.level = 2;
            }
        }
        
        BMAMovieViewController* theMoviePlayer = [[BMAMovieViewController alloc] initWithPath:[[NSBundle mainBundle] pathForResource:@"win" ofType:@"mp4"]];
        
        [self presentModalViewController:theMoviePlayer animated:NO];
        
        NSLog(@"gagné");
        
        [self refreshGame];
    }
    
    
    
    /*
    [UIView animateWithDuration:0.5 animations:^{
        congratsView.alpha = 1;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.1 delay:0.7 options:0 animations:^{
            congratsView.alpha = 0;
        } completion:^(BOOL finished) {
            
        }];
    }];
    */
    
    /*
    NSString *alertString;
    if (result == true) {
        alertString = @"Tu as gagné";
        [self initGame];
    } else {
        alertString = @"Tu as perdu";
    }
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Result" 
                                                    message:alertString
                                                   delegate:nil 
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
    */
}

- (IBAction) buttonIllustration1Pushed {
    DLOG(@"buttonIllustration1Pushed");
    DLOG(@"%d - %d", emotionIllustration1, currentEmotion);
    if (emotionIllustration1 == currentEmotion) {
        [self result:true];
    } else {
        [self result:false];
    }
}

- (IBAction) buttonIllustration2Pushed {
    DLOG(@"buttonIllustration2Pushed");
    DLOG(@"%d - %d", emotionIllustration2, currentEmotion);
    if (emotionIllustration2 == currentEmotion) {
        [self result:true];
    } else {
        [self result:false];
    }
}

- (IBAction) buttonIllustration3Pushed {
    DLOG(@"buttonIllustration3Pushed");
    if (emotionIllustration3 == currentEmotion) {
        [self result:true];
    } else {
        [self result:false];
    }
}

- (IBAction) buttonIllustration4Pushed {
    DLOG(@"buttonIllustration4Pushed");
    if (emotionIllustration4 == currentEmotion) {
        [self result:true];
    } else {
        [self result:false];
    }
}

@end
